using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Lucene.Linq.Search;

namespace Lucene.Linq.VSTests
{
    [TestClass]
    public class TestFieldAnalyzer
    {
        [TestMethod]
        public void the_same_analyzer_should_be_used_for_indexing_querying_and_projection()
        {
            int inty = 245;
            double doubly = 100.23d;
            float fl = 12.25f;
            string str = "string string string";

            // set up
            var item = new Sample
                           {
                               CaptainInteger = inty,
                               SargeantInteger = fl,
                               BobDoublina = doubly,
                               MrString = str
                           };

            var checker = new Action<Sample>((s) =>
                                                 {
                                                     Assert.AreEqual(s.CaptainInteger, inty);
                                                     Assert.AreEqual(s.BobDoublina, doubly);
                                                     Assert.AreEqual(s.SargeantInteger, fl);
                                                 });

            using (var idx = new Index<Sample> {item})
            {
                // execute
                IQueryable<Sample> results = from i in idx
                                             where i.CaptainInteger == 245
                                             select i;

                // expect
                Assert.AreEqual(results.Count(), 1);

                Sample element = results.ElementAt(0);
                checker(element);
                element = results.First();
                checker(element);
                element = results.Last();
                checker(element);
            }
        }
    }
}